package com.mvp.shq.washer.ui.setting;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.rxbus.ThreadMode;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.CustomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity<SettingPresenter, SettingModel> implements SettingConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    private CustomDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context,R.color.white),0);
    }


    @OnClick({R.id.back, R.id.arl_reset_pwd, R.id.arl_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.arl_reset_pwd:
                JumpUtil.toResetPwd(context);
                break;
            case R.id.arl_loginout:
                CustomDialog.Builder builder = new CustomDialog.Builder(context);
                if (dialog == null){
                    dialog = builder.setMessage("退出后,将无法获取您的洗衣状态")
                            .setNegativeButton("确认退出", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    showLoadingDialog(true);
                                    presenter.loginout();
                                }
                            })
                            .setPositiveButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            }).createTwoButtonDialog();
                }
                dialog.show();
                break;
        }
    }

    @Override
    @Subscribe(code = EventCode.DEL_JPUSH_ALIAS,threadMode = ThreadMode.MAIN)
    public void loginoutSuccess() {
        hideLoadingDialog();
        JumpUtil.toLogin(context);
        SpUtil.setUser(null);
        RxBus.get().send(EventCode.LOGIN_OUT);
        finish();
    }

    @Override
    @Subscribe(code = EventCode.DEL_JPUSH_ALIAS,threadMode = ThreadMode.MAIN)
    public void loginoutFail(String msg) {
        hideLoadingDialog();
        JumpUtil.toLogin(context);
        SpUtil.setUser(null);
        RxBus.get().send(EventCode.LOGIN_OUT);
        finish();
    }
}
